<template>
  <div>
      <!--    搜索栏-->
      <el-card id="serch">
          <el-row>
              <el-col :span="18">
                  <!-- <el-input v-model="searchModel.uid" placeholder="请输入学号"></el-input> -->
                  <el-input v-model="searchModel.name" placeholder="请输入教师姓名"></el-input>
                  <el-select v-model="searchModel.value" clearable placeholder="请选择职称">
                      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                  </el-select>
                  <el-button @click="getTeacherList" type="primary" icon="el-icon-search">搜索</el-button>
              </el-col>
              <el-col :span="6" align="right">
                  <el-button @click="OpenEditUI(null)" type="primary" icon="el-icon-plus">添加</el-button>
              </el-col>
          </el-row>
      </el-card>
      <!--    结果栏-->
      <el-card>
          <el-table :data="TeacherList" stripe style="width: 100%">
              <el-table-column label="#" width="80" align="center">
                  <template slot-scope="scope">
                      {{ (searchModel.pageNo - 1) * searchModel.pageSize + scope.$index + 1 }}
                  </template>

              </el-table-column>
              <!-- <el-table-column prop="id" label="id" width="50">
              </el-table-column> -->
              <el-table-column prop="name" label="姓名" width="180" align="center">
              </el-table-column>
              <el-table-column prop="gender" label="性别" align="center">
              </el-table-column>
              <el-table-column prop="tno" label="工号" align="center">
              </el-table-column>
              <el-table-column prop="zhicheng" label="职称" align="center">
              </el-table-column>
              <el-table-column prop="telephone" label="电话" align="center">
              </el-table-column>
              <el-table-column prop="address" label="住址" align="center">
              </el-table-column>
              <el-table-column label="操作">
                  <template slot-scope="scope">
                      <el-button size="mini" @click="OpenEditUI(scope.row.tno)">编辑
                      </el-button>
                      <el-button size="mini" type="danger" @click="deleteStudent(scope.row)">删除
                      </el-button>
                  </template>
              </el-table-column>
          </el-table>
      </el-card>
      <div class="block">
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
              :current-page="searchModel.pageNo" :page-sizes="[5, 10, 15, 20]" :page-size="searchModel.pageSize"
              layout="total, sizes, prev, pager, next, jumper" :total="total">
          </el-pagination>

          <!-- 用户信息编辑对话框 -->
          <el-dialog @close="clearForm" :title="title" :visible.sync="dialogFormVisible">
              <el-form :model="teacherForm">
                  <el-form-item label="姓名" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.name" autocomplete="off"></el-input>
                  </el-form-item>
                  <!-- <el-form-item label="性别" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.gender" autocomplete="off"></el-input>
                  </el-form-item> -->
                  <el-form-item label="性别" :label-width="formLabelWidth">
                      <el-select v-model="teacherForm.gender" placeholder="请选择性别">
                          <el-option v-for="item in gender" :key="item.value" :label="item.label" :value="item.value">
                          </el-option>
                      </el-select>
                  </el-form-item>
                  <el-form-item label="工号" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.tno" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="电话" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.telephone" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="住址" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.address" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="权限" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.address" autocomplete="off"></el-input>
                  </el-form-item>
                  <el-form-item label="描述" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.address" autocomplete="off"></el-input>
                  </el-form-item>
                  <!-- <el-form-item label="职称" :label-width="formLabelWidth">
                      <el-input v-model="teacherForm.address" autocomplete="off"></el-input>
                  </el-form-item> -->
                  <!-- <el-form-item label="职称" :label-width="formLabelWidth">
                      <el-select v-model="teacherForm.zhicheng" placeholder="请选择职称   ">
                          <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                          </el-option>
                      </el-select>
                  </el-form-item> -->

              </el-form>
              <div slot="footer" class="dialog-footer">
                  <el-button @click="dialogFormVisible = false">取 消</el-button>
                  <!-- <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button> -->
                  <el-button type="primary" @click="saveUser">确 定</el-button>
              </div>
          </el-dialog>
      </div>
  </div>
</template>


<script>
import teacherApi from '@/api/tacher'

export default {
  data() {
      return {
          formLabelWidth: '130px',
          teacherForm: {
              value: '',
          },
          title: "",
          dialogFormVisible: false,
          total: 0,
          searchModel: {
              pageNo: 1,
              pageSize: 5,
              value: '',
          },
          TeacherList: [],
          options: [],
          gender: [{
              value: '男',
              label: '男'
          },{
              value: '女',
              label: '女'
          }
              
          ]

      }
  },
  methods: {
      deleteStudent(student) {
          teacherApi.deleteById(student.id).then(response => {
              this.$message({
                  message: response.message
              });
              this.getTeacherList();
              // this.getTeacherList();
          });

      },
      getzhicheng() {

          fetch("http://localhost:9001/teacher/xialacaidan").then(response => response.json()).then(res => {
              // res.forEach(item => {
              this.options = res
          });
      },
      saveUser() {
          teacherApi.addTeacher(this.teacherForm).then(response => {
              this.$message({
                  message: response.message,
                  type: 'success'
              });
              this.dialogFormVisible = false;
              this.getTeacherList();
          })
          // alert(123)
      },
      clearForm() {
          this.teacherForm = {};

      },
      // OpenEditUI(){
      OpenEditUI(uid) {
          if (uid == null) {
              this.title = "新增教师用户";
          } else {
              this.title = "修改教师信息";
              teacherApi.getById(uid).then(response => {
                  this.teacherForm = response.data;
              });

          }

          this.dialogFormVisible = true;
      },
      handleEdit(index, row) {
          console.log(index, row);
      },
      handleDelete(index, row) {
          console.log(index, row);
      },
      handleSizeChange(pageSize) {
          this.searchModel.pageSize = pageSize;
          this.getTeacherList();
      },
      handleCurrentChange(pageNo) {
          this.searchModel.pageNo = pageNo;
          this.getTeacherList();
      },
      getTeacherList() {
          teacherApi.getAllTeacher(this.searchModel).then(response => {
              this.TeacherList = response.data.records;
              this.total = response.data.total;
          });
      }
  },
  created() {
      this.getTeacherList();
      this.getzhicheng();
  }
}


</script>
<style>
#serch .el-input {
  width: 200px;
  margin-right: 30px;
}

.el-dialog .el-input {
  width: 80%;
}
</style>